package com.shell.customer.base.service.impl;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.auth.AuthUser;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.customer.base.CusEvent;
import com.shell.customer.base.dao.CusRecDao;
import com.shell.customer.base.dao.CusRelDao;
import com.shell.customer.base.dao.CustomerDao;
import com.shell.customer.base.rx.AuthEntAcctForCusSubcriber;
import com.shell.customer.base.rx.OpenCusAcctForEditSubscriber;
import com.shell.customer.base.rx.PushUserAndOrganForEditSubscriber;
import com.shell.customer.base.rx.SaveCusMsgSubscriber;
import com.shell.customer.base.rx.SetBaseTagSubscriber;
import com.shell.customer.base.rx.SetCusUserAndOrganSubscriber;
import com.shell.customer.base.rx.SyncEntUserSubscriber;
import com.shell.customer.base.rx.UpdateCusAcctSubscriber;
import com.shell.customer.base.rx.UpdateCusExtraSubscriber;
import com.shell.customer.base.rx.UpdateCusForEditSubscriber;
import com.shell.customer.base.service.CusCheckService;
import com.shell.customer.base.service.CustomerService;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.po.CusRec;
import com.shell.feign.PubUserExtraFeign;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.ParameterHelper;
import com.shell.governor.modules.vo.ParameterVo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("customerService")
public class CustomerServiceImpl extends PersistentServiceImpl<CusRec> implements CustomerService {
	@Autowired
	CustomerDao customerDao;
	@Autowired
	CusCheckService cusCheckService;
	@Autowired
	UpdateCusForEditSubscriber updateCusSubscriber;
	@Autowired
	SetCusUserAndOrganSubscriber setCusUserAndOrganSubscriber;
	@Autowired
	UpdateCusAcctSubscriber updateCusAcctSubscriber;
	@Autowired
	OpenCusAcctForEditSubscriber openCusAcctForEditSubscriber;
	@Autowired
	PushUserAndOrganForEditSubscriber pushUserAndOrganForEditSubscriber;
	@Autowired
	UpdateCusExtraSubscriber cusExtraSubscriber;
	@Autowired
	SetBaseTagSubscriber setBaseTagSubscriber;
	@Autowired
	CusRelDao cusRelDao;
	@Autowired
	CusRecDao cusRecDao;
	@Autowired
	private SyncEntUserSubscriber syncEntUserSubscriber;
	@Autowired
	private SaveCusMsgSubscriber saveCusMsgSubscriber;
	@Autowired
	private PubUserExtraFeign pubUserExtraFeign;
	@Autowired
	private AuthEntAcctForCusSubcriber authEntAcctForCusSubcriber;

	public boolean saveCus(CusRecVo customercVo) {
		String msg = this.cusCheckService.checkCusForUpdate(customercVo);
		if (StringUtil.notEmpty(msg)) {
			throw new UnCheckMsgException(msg);
		}
		boolean isAdd = StringUtil.emptyAndNull(customercVo.getCusId());
		CusEvent event = new CusEvent();
		event.setCusRecVo(customercVo);
		event.setAdd(isAdd);

		Observable<CusEvent> observers = Observable.just(event);

		observers.subscribe(this.setCusUserAndOrganSubscriber);
		observers.subscribe(this.updateCusSubscriber);
		observers.subscribe(this.updateCusAcctSubscriber);
		observers.subscribe(this.authEntAcctForCusSubcriber);

		observers.subscribe(this.openCusAcctForEditSubscriber);
		observers.subscribe(this.setBaseTagSubscriber);
		observers.subscribe(this.pushUserAndOrganForEditSubscriber);
		observers.subscribe(this.syncEntUserSubscriber);

		observers.subscribe(this.saveCusMsgSubscriber);
		return isAdd;
	}

	@Pagination
	public List<CusRecVo> findAllCus(PageLimit limit, String searchValue, String[] cusTypes, boolean add,
			String tagCode) {
		if (StringUtil.notTrimEmpty(searchValue)) {
			searchValue = StringUtil.getParameterLike(searchValue);
		}
		String[] states = { "E", "6" };
		if (StringUtil.emptyAndNull(tagCode)) {
			return this.customerDao.findAllCus(states, searchValue, cusTypes, add);
		}
		return this.customerDao.findAllCusByTagCode(states, searchValue, null, add, tagCode);
	}

	@Pagination
	public List<CusRecVo> findAllCus(PageLimit limit, String searchValue, String[] cusType, boolean add, String tagCode,
			String excludeTagCode) {
		if (StringUtil.notTrimEmpty(searchValue)) {
			searchValue = StringUtil.getParameterLike(searchValue);
		}
		String[] states = { "E", "6" };

		return this.customerDao.findAllCusByExcludeTagCode(states, searchValue, cusType, add, tagCode, excludeTagCode);
	}

	public List<CusRec> selectFocalEnts(String cusType, String entId) {
		return this.cusRecDao.selectFocalEnts(cusType, entId, "E");
	}

	public List<CusRec> selectCusByGroupIdAndTagCode(String grpId, String tagCodes) {
		String[] tagCodeArr = tagCodes.split(",");
		String[] states = { "E", "F" };
		return this.cusRecDao.selectCusByGroupIdAndTagCode(states, grpId, tagCodeArr);
	}

	public List<CusRec> selectByGroupIdAndRelType(String grpId, String relType) {
		return this.cusRecDao.selectByGroupIdAndRelType(grpId, relType);
	}

	@Pagination
	public List<CusRecVo> findCusForGrp(PageLimit limit, String grpId, String searchValue, String tagCode,
			String cusLvl) {
		if (StringUtil.notTrimEmpty(searchValue)) {
			searchValue = StringUtil.getParameterLike(searchValue);
		}
		String[] tagCodes = { "FOCAL_CO", "MEMBER", "SUPPLIER" };
		if (StringUtil.notEmpty(tagCode)) {
			tagCodes = new String[1];
			tagCodes[0] = tagCode;
		}
		String[] cusStates = { "E", "F" };
		return this.customerDao.findCusForGrp(grpId, searchValue, cusStates, tagCodes, null, cusLvl);
	}

	public List<CusRec> selectFocalEntsForGroup(String focalId) {
		return this.cusRecDao.selectFocalEntsForGroup("E", "ENTERPRISE", focalId,
				new String[] { "FOCAL_CO", "ARTISAN" });
	}

	@Pagination
	public List<CusRecVo> findAllCusForAdmin(PageLimit limit, String grpId, String searchValue) {
		if (StringUtil.notTrimEmpty(searchValue)) {
			searchValue = StringUtil.getParameterLike(searchValue);
		}
		String[] cusStates = { "E" };
		return this.customerDao.findCusForGrp(grpId, searchValue, cusStates, null, null, null);
	}

	public RetMsg frozen(String cusId) {
		CusRec cusRec = (CusRec) this.cusRecDao.selectByPrimaryKey(cusId);
		cusRec.setCusState("F");
		this.cusRecDao.updateByPrimaryKey(cusRec);
		this.pubUserExtraFeign.updateUserStateForEnt(cusId, "F");
		return new RetMsg("success", true, "��������������");
	}

	public RetMsg thaw(String cusId) {
		CusRec cusRec = (CusRec) this.cusRecDao.selectByPrimaryKey(cusId);
		cusRec.setCusState("E");
		this.cusRecDao.updateByPrimaryKey(cusRec);
		this.pubUserExtraFeign.updateUserStateForEnt(cusId, "E");
		return new RetMsg("success", true, "��������������");
	}

	public Map<String, String> getAppShareInfo() {
		Map<String, String> ret = new HashMap();
		String userId = CommonDataUtil.getUserId();
		String entId = CommonDataUtil.getCurEntId();
		ret.put("userName", CommonDataUtil.getCurUser().getUserName());
		ret.put("cusName", CommonDataUtil.getCurEntName());
		ret.put("inviteCode", this.pubUserExtraFeign.getInviteCode(userId, entId));
		ParameterVo parameter = ParameterHelper.getCommonParameter("PLAT_NAME");
		if ((parameter == null) || (StringUtil.emptyAndNull(parameter.getParmValue()))) {
			throw new UnCheckMsgException("��������������");
		}
		ret.put("platformName", parameter.getParmValue());

		List<CusRec> recs = this.cusRecDao.getPlatform(null, "OPERATOR");
		CusRec platform = null;
		if ((recs != null) && (recs.size() > 0)) {
			platform = (CusRec) recs.get(0);
		}
		if (platform == null) {
			throw new UnCheckMsgException("����������������");
		}
		ret.put("platformOperatorName", platform.getCusName());
		parameter = ParameterHelper.getCommonParameter("REGISTER_URL");
		if ((parameter == null) || (StringUtil.emptyAndNull(parameter.getParmValue()))) {
			throw new UnCheckMsgException("��������������");
		}
		ret.put("registerUrl", parameter.getParmValue());
		return ret;
	}
}
